home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / lisp / packages / shell-font.el.z / shell-font.el
Encoding:
Text File  |  1998-05-21  |  5.3 KB  |  143 lines

  1. ;; Decorate a shell buffer with fonts.
  2. ;; Copyright (C) 1992, 1993, 1994 Free Software Foundation, Inc.
  3.  
  4. ;; This file is part of XEmacs.
  5.  
  6. ;; XEmacs is free software; you can redistribute it and/or modify it
  7. ;; under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation; either version 2, or (at your option)
  9. ;; any later version.
  10.  
  11. ;; XEmacs is distributed in the hope that it will be useful, but
  12. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14. ;; General Public License for more details.
  15.  
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with XEmacs; see the file COPYING.  If not, write to the 
  18. ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  19. ;; Boston, MA 02111-1307, USA.
  20.  
  21. ;;; Synched up with: Not in FSF.
  22.  
  23. ;; Do this: (add-hook 'shell-mode-hook 'install-shell-fonts) 
  24. ;; and the prompt in your shell-buffers will appear bold-italic, process
  25. ;; output will appear in normal face, and typein will appear in bold.
  26. ;;
  27. ;; The faces shell-prompt, shell-input and shell-output can be modified
  28. ;; as desired, for example, (copy-face 'italic 'shell-prompt).
  29.  
  30. ;; Written by Jamie Zawinski, overhauled by Eric Benson.
  31.  
  32. ;; TODO:
  33. ;; =====
  34. ;; Parse ANSI/VT100 escape sequences to turn on underlining/boldface/etc.
  35. ;; Automatically run nuke-nroff-bs?
  36.  
  37.  
  38. (require 'text-props)    ; for put-nonduplicable-text-property
  39.  
  40. (make-face 'shell-prompt)
  41. (if (not (face-differs-from-default-p 'shell-prompt))
  42.     (copy-face 'bold-italic 'shell-prompt))
  43.  
  44. (make-face 'shell-input)
  45. (if (not (face-differs-from-default-p 'shell-input))
  46.     (copy-face 'bold 'shell-input))
  47.  
  48. (make-face 'shell-output)
  49. (if (not (face-differs-from-default-p 'shell-output))
  50.     (progn (make-face-unbold 'shell-output)
  51.        (make-face-unitalic 'shell-output)
  52.        (set-face-underline-p 'shell-output nil)))
  53.  
  54. (defvar shell-font-read-only-prompt nil
  55.   "*Set all shell prompts to be read-only")
  56.  
  57. (defvar shell-font-current-face 'shell-input)
  58.  
  59. (defun shell-font-fontify-region (start end delete-count)
  60.   ;; for use as an element of after-change-functions; fontifies the inserted text.
  61.   (if (= start end)
  62.       nil
  63. ;    ;; This creates lots of extents (one per user-typed character)
  64. ;    ;; which is wasteful of memory.
  65. ;    (let ((e (make-extent start end)))
  66. ;      (set-extent-face e shell-font-current-face)
  67. ;      (set-extent-property e 'shell-font t))
  68.  
  69.     ;; This efficiently merges extents
  70.     (put-nonduplicable-text-property start end 'face shell-font-current-face)
  71.     (and shell-font-read-only-prompt
  72.      (eq shell-font-current-face 'shell-prompt)
  73.      (put-nonduplicable-text-property start end 'read-only t))
  74.     ))
  75.  
  76. (defun shell-font-hack-prompt (limit)
  77.   "Search backward from point-max for text matching the comint-prompt-regexp,
  78. and put it in the `shell-prompt' face.  LIMIT is the left bound of the search."
  79.   (save-excursion
  80.     (goto-char (point-max))
  81.     (save-match-data
  82.      (cond ((re-search-backward comint-prompt-regexp limit t)
  83.         (goto-char (match-end 0))
  84.         (cond ((= (point) (point-max))
  85.            (skip-chars-backward " \t")
  86.            (let ((shell-font-current-face 'shell-prompt))
  87.              (shell-font-fontify-region
  88.               (match-beginning 0) (point) 0)))))))))
  89.  
  90.  
  91. (defvar shell-font-process-filter nil
  92.   "In an interaction buffer with shell-font, this is the original proc filter.
  93. shell-font encapsulates this.")
  94.  
  95. (defun shell-font-process-filter (proc string)
  96.   "Invoke the original process filter, then set fonts on the output.
  97. The original filter is in the buffer-local variable shell-font-process-filter."
  98.   (let ((cb (current-buffer))
  99.     (pb (process-buffer proc)))
  100.     (if (null pb)
  101.     ;; If the proc has no buffer, leave it alone.
  102.     (funcall shell-font-process-filter proc string)
  103.       ;; Don't do save excursion because some proc filters want to change
  104.       ;; the buffer's point.
  105.       (set-buffer pb)
  106.       (let ((p (marker-position (process-mark proc))))
  107.     (prog1
  108.         ;; this let must not be around the `set-buffer' call.
  109.         (let ((shell-font-current-face 'shell-output))
  110.           (funcall shell-font-process-filter proc string))
  111.       (shell-font-hack-prompt p)
  112.       (set-buffer cb))))))
  113.  
  114. ;;;###autoload
  115. (defun install-shell-fonts ()
  116.   "Decorate the current interaction buffer with fonts.
  117. This uses the faces called `shell-prompt', `shell-input' and `shell-output';
  118. you can alter the graphical attributes of those with the normal
  119. face-manipulation functions."
  120.   (let* ((proc (or (get-buffer-process (current-buffer))
  121.            (error "no process in %S" (current-buffer))))
  122.      (old (or (process-filter proc)
  123.           (error "no process filter on %S" proc))))
  124.     (make-local-variable 'after-change-functions)
  125.     (add-hook 'after-change-functions 'shell-font-fontify-region)
  126.     (make-local-variable 'shell-font-current-face)
  127.     (setq shell-font-current-face 'shell-input)
  128.     (make-local-variable 'shell-font-process-filter)
  129.     (or (eq old 'shell-font-process-filter) ; already set
  130.     (setq shell-font-process-filter old))
  131.     (set-process-filter proc 'shell-font-process-filter))
  132.   nil)
  133.  
  134. (add-hook 'shell-mode-hook    'install-shell-fonts)
  135. (add-hook 'telnet-mode-hook    'install-shell-fonts)
  136. (add-hook 'gdb-mode-hook    'install-shell-fonts)
  137.  
  138. ;; for compatibility with the 19.8 version
  139. ;(fset 'install-shell-font-prompt 'install-shell-fonts)
  140. (make-obsolete 'install-shell-font-prompt 'install-shell-fonts)
  141.  
  142. (provide 'shell-font)
  143.